#include "LinuxModuleWidget.h"
#include "dbusutil.h"

#include <QComboBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QGroupBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QHBoxLayout>
#include <QVBoxLayout>

#include <QListWidget>
#include <QMenu>
#include <QScrollArea>

#include <CommandUtil/CommandUtil.h>
#include <QtPrivateDeclar/QtPrivateDeclar.h>

LinuxModuleWidget::LinuxModuleWidget(QWidget *parent) : QWidget(parent) {
    QVBoxLayout *Leftlayout = new QVBoxLayout();
// left

// 1.1
    QGroupBox *leftBox = new QGroupBox("信息展示");
    QVBoxLayout *leftBoxLayout = new QVBoxLayout(leftBox);
    Leftlayout->addWidget(leftBox);

        // 查询软件包
     // 搜索关键词
    QLabel *searchKeyLabel = new QLabel("关键词");
    QLineEdit *searchKey = new QLineEdit();
    QHBoxLayout* searchKeyLayout = new QHBoxLayout();
    searchKeyLayout->addWidget(searchKeyLabel);
    searchKeyLayout->addWidget(searchKey);

    // 数据显示
    QListWidget *keyListWidget = new QListWidget();
    keyListWidget->setSortingEnabled(true);

    // end
    // leftBoxLayout->addLayout(searchKeyFileLayout);
    leftBoxLayout->addLayout(searchKeyLayout);
    leftBoxLayout->addWidget(keyListWidget);

    QVBoxLayout *Rightlayout = new QVBoxLayout();
// right

// 2.1
    QGroupBox *rightBox = new QGroupBox("信息展示");
    QVBoxLayout *rightBoxLayout = new QVBoxLayout(rightBox);
    Rightlayout->addWidget(rightBox);

    // 显示驱动信息

    // filename
    QLabel *filenameLabel = new QLabel("filename");
    QLineEdit *filenameEdit = new QLineEdit();
    // firmware
    QLabel *firmwareLabel = new QLabel("firmware");
    QLineEdit *firmwareEdit = new QLineEdit();
    // license
    QLabel *licenseLabel = new QLabel("license");
    QLineEdit *licenseEdit = new QLineEdit();
    // softdep
    QLabel *softdepLabel = new QLabel("softdep");
    QLineEdit *softdepEdit = new QLineEdit();
    // description
    QLabel *descriptionLabel = new QLabel("description");
    QLineEdit *descriptionEdit = new QLineEdit();
    // author
    QLabel *authorLabel = new QLabel("author");
    QLineEdit *authorEdit = new QLineEdit();
    // srcversion
    QLabel *srcversionLabel = new QLabel("srcversion");
    QLineEdit *srcversionEdit = new QLineEdit();
    // retpoline
    QLabel *retpolineLabel = new QLabel("retpoline");
    QLineEdit *retpolineEdit = new QLineEdit();
    // depends
    QLabel *dependsLabel = new QLabel("depends");
    QLineEdit *dependsEdit = new QLineEdit();
    // intree
    QLabel *intreeLabel = new QLabel("intree");
    QLineEdit *intreeEdit = new QLineEdit();
    // vermagic
    QLabel *vermagicLabel = new QLabel("vermagic");
    QLineEdit *vermagicEdit = new QLineEdit();
    // sig_id
    QLabel *sig_idLabel = new QLabel("sig_id");
    QLineEdit *sig_idEdit = new QLineEdit();
    // signer
    QLabel *signerLabel = new QLabel("signer");
    QLineEdit *signerEdit = new QLineEdit();
    // sig_key
    QLabel *sig_keyLabel = new QLabel("sig_key");
    QLineEdit *sig_keyEdit = new QLineEdit();
    // sig_hashalgo
    QLabel *sig_hashalgoLabel = new QLabel("sig_hashalgo");
    QLineEdit *sig_hashalgoEdit = new QLineEdit();
    // signature;
    QLabel *signatureLabel = new QLabel("signature");
    QLineEdit *signatureEdit = new QLineEdit();
    // alias  
    QLabel *aliasLabel = new QLabel("alias");
    QLineEdit *aliasEdit = new QLineEdit();
    // parm
    QLabel *parmLabel = new QLabel("parm");
    QLineEdit *parmEdit = new QLineEdit();


    filenameEdit->setText("");
    firmwareEdit->setText("");
    licenseEdit->setText("");
    softdepEdit->setText("");
    descriptionEdit->setText("");
    authorEdit->setText("");
    srcversionEdit->setText("");
    retpolineEdit->setText("");
    dependsEdit->setText("");
    intreeEdit->setText("");
    vermagicEdit->setText("");
    sig_idEdit->setText("");
    signerEdit->setText("");
    sig_keyEdit->setText("");
    sig_hashalgoEdit->setText("");
    signatureEdit->setText("");
    aliasEdit->setText("");
    parmEdit->setText("");


    QGridLayout *moduleInfo = new QGridLayout();
    moduleInfo->addWidget(filenameLabel, 0, 0);
    moduleInfo->addWidget(filenameEdit, 0, 1);
    moduleInfo->addWidget(firmwareLabel, 1, 0);
    moduleInfo->addWidget(firmwareEdit, 1, 1);
    moduleInfo->addWidget(licenseLabel, 2, 0);
    moduleInfo->addWidget(licenseEdit, 2, 1);
    moduleInfo->addWidget(softdepLabel, 3, 0);
    moduleInfo->addWidget(softdepEdit, 3, 1);
    moduleInfo->addWidget(descriptionLabel, 4, 0);
    moduleInfo->addWidget(descriptionEdit, 4, 1);
    moduleInfo->addWidget(authorLabel, 5, 0);
    moduleInfo->addWidget(authorEdit, 5, 1);
    moduleInfo->addWidget(srcversionLabel, 6, 0);
    moduleInfo->addWidget(srcversionEdit, 6, 1);
    moduleInfo->addWidget(retpolineLabel, 7, 0);
    moduleInfo->addWidget(retpolineEdit, 7, 1);
    moduleInfo->addWidget(dependsLabel, 8, 0);
    moduleInfo->addWidget(dependsEdit, 8, 1);
    moduleInfo->addWidget(intreeLabel, 9, 0);
    moduleInfo->addWidget(intreeEdit, 9, 1);
    moduleInfo->addWidget(vermagicLabel, 10, 0);
    moduleInfo->addWidget(vermagicEdit, 10, 1);
    moduleInfo->addWidget(sig_idLabel, 11, 0);
    moduleInfo->addWidget(sig_idEdit, 11, 1);
    moduleInfo->addWidget(signerLabel, 12, 0);
    moduleInfo->addWidget(signerEdit, 12, 1);
    moduleInfo->addWidget(sig_keyLabel, 13, 0);
    moduleInfo->addWidget(sig_keyEdit, 13, 1);
    moduleInfo->addWidget(sig_hashalgoLabel, 14, 0);
    moduleInfo->addWidget(sig_hashalgoEdit, 14, 1);
    moduleInfo->addWidget(signatureLabel, 15, 0);
    moduleInfo->addWidget(signatureEdit, 15, 1);
    moduleInfo->addWidget(aliasLabel, 16, 0);
    moduleInfo->addWidget(aliasEdit, 16, 1);
    moduleInfo->addWidget(parmLabel, 17, 0);
    moduleInfo->addWidget(parmEdit, 17, 1);

    // end
    rightBoxLayout->addLayout(moduleInfo);
    rightBoxLayout->addStretch();

    QHBoxLayout *layout = new QHBoxLayout(this);
    layout->addLayout(Leftlayout,1);
    layout->addLayout(Rightlayout,1);

    resize(1200, 800);

// do 
    connect(searchKey, &QLineEdit::textChanged, [=](const QString &text){
        keyListWidget->clear();
        for (auto module : linuxModules) {
            // QStringList words = module.split("");

            bool found = true;
            for (auto &key : text.split(" "))
            {
                if (!module.contains(key, Qt::CaseSensitivity::CaseInsensitive)) {
                    found = false;
                }
            }
            if (found) {
                // if (words.length() >= 2) {
                    // keyListWidget->addItem(QString("%1/%2").arg(words[0]).arg(words[1]));
                // } else {
                    auto declar = new QtPrivateDeclar();
                    declar->setModule(module);
                    keyListWidget->addItem(QString("%1 - %2").arg(module).arg(declar->getFilename()));
                    // keyListWidget->addItem(module);
                // }
            }
        }
    });

    linuxVersion = CommandUtil::RunCommand("uname", QStringList() << "-r").trimmed();

    for (auto &line : CommandUtil::RunCommand("lsmod", QStringList()).split("\n"))
    {
        if (line.startsWith("Module")) continue;
        QStringList words = line.split(" ");
        if (words.size() >= 1) {
            linuxModules.append(words[0]);
            auto module = new QtPrivateDeclar();
            module->setModule(words[0]);
            keyListWidget->addItem(QString("%1 - %2").arg(words[0]).arg(module->getFilename()));
        }
    }
    // keyListWidget->addItems(linuxModules);

    keyListWidget->setContextMenuPolicy(Qt::CustomContextMenu);
    connect(keyListWidget, &QListWidget::customContextMenuRequested, this, [keyListWidget, this](const QPoint &pos){
        QMenu menu;
        menu.addAction("打开所在位置", [keyListWidget](){
//            keyListWidget->currentItem().
            QtPrivateDeclar declar;
            declar.setModule(keyListWidget->currentItem()->text().split(" ")[0]);
            DBusUtil::showFileLocation(declar.getFilename());
        });

        menu.exec(QCursor::pos());
    });

    connect(keyListWidget, &QListWidget::currentTextChanged, [=](const QString &currentText){
        QtPrivateDeclar *declar  = new QtPrivateDeclar;
        declar->setModule(currentText.split(" ")[0]);
        filenameEdit->setText(declar->getFilename());
        firmwareEdit->setText(declar->getFirmware().join(" "));
        licenseEdit->setText(declar->getLicense());
        softdepEdit->setText(declar->getSoftdep());
        descriptionEdit->setText(declar->getDescription());
        authorEdit->setText(declar->getAuthor());
        srcversionEdit->setText(declar->getSrcversion());
        retpolineEdit->setText(declar->getRetpoline());
        dependsEdit->setText(declar->getDepends());
        intreeEdit->setText(declar->getIntree());
        vermagicEdit->setText(declar->getVermagic());
        sig_idEdit->setText(declar->getSig_id());
        signerEdit->setText(declar->getSigner());
        sig_keyEdit->setText(declar->getSig_key());
        sig_hashalgoEdit->setText(declar->getSig_hashalgo());
        signatureEdit->setText(declar->getSignature());
        aliasEdit->setText(declar->getAlias().join(" "));
        parmEdit->setText(declar->getParm().join(" "));


        if (filenameEdit->text().length() == 0) {
            moduleInfo->itemAt(0)->widget()->setHidden(true);
            moduleInfo->itemAt(1)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(0)->widget()->setHidden(false);
            moduleInfo->itemAt(1)->widget()->setHidden(false);
        }
        if (firmwareEdit->text().length() == 0) {
            moduleInfo->itemAt(2)->widget()->setHidden(true);
            moduleInfo->itemAt(3)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(2)->widget()->setHidden(false);
            moduleInfo->itemAt(3)->widget()->setHidden(false);
        }
        if (licenseEdit->text().length() == 0) {
            moduleInfo->itemAt(4)->widget()->setHidden(true);
            moduleInfo->itemAt(5)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(4)->widget()->setHidden(false);
            moduleInfo->itemAt(5)->widget()->setHidden(false);
        }
        if (softdepEdit->text().length() == 0) {
            moduleInfo->itemAt(6)->widget()->setHidden(true);
            moduleInfo->itemAt(7)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(6)->widget()->setHidden(false);
            moduleInfo->itemAt(7)->widget()->setHidden(false);
        }
        if (descriptionEdit->text().length() == 0) {
            moduleInfo->itemAt(8)->widget()->setHidden(true);
            moduleInfo->itemAt(9)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(8)->widget()->setHidden(false);
            moduleInfo->itemAt(9)->widget()->setHidden(false);
        }
        if (authorEdit->text().length() == 0) {
            moduleInfo->itemAt(10)->widget()->setHidden(true);
            moduleInfo->itemAt(11)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(10)->widget()->setHidden(false);
            moduleInfo->itemAt(11)->widget()->setHidden(false);
        }
        if (srcversionEdit->text().length() == 0) {
            moduleInfo->itemAt(12)->widget()->setHidden(true);
            moduleInfo->itemAt(13)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(12)->widget()->setHidden(false);
            moduleInfo->itemAt(13)->widget()->setHidden(false);
        }
        if (retpolineEdit->text().length() == 0) {
            moduleInfo->itemAt(14)->widget()->setHidden(true);
            moduleInfo->itemAt(15)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(14)->widget()->setHidden(false);
            moduleInfo->itemAt(15)->widget()->setHidden(false);
        }
        if (dependsEdit->text().length() == 0) {
            moduleInfo->itemAt(16)->widget()->setHidden(true);
            moduleInfo->itemAt(17)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(16)->widget()->setHidden(false);
            moduleInfo->itemAt(17)->widget()->setHidden(false);
        }
        if (intreeEdit->text().length() == 0) {
            moduleInfo->itemAt(18)->widget()->setHidden(true);
            moduleInfo->itemAt(19)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(18)->widget()->setHidden(false);
            moduleInfo->itemAt(19)->widget()->setHidden(false);
        }
        if (vermagicEdit->text().length() == 0) {
            moduleInfo->itemAt(20)->widget()->setHidden(true);
            moduleInfo->itemAt(21)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(20)->widget()->setHidden(false);
            moduleInfo->itemAt(21)->widget()->setHidden(false);
        }
        if (sig_idEdit->text().length() == 0) {
            moduleInfo->itemAt(22)->widget()->setHidden(true);
            moduleInfo->itemAt(23)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(22)->widget()->setHidden(false);
            moduleInfo->itemAt(23)->widget()->setHidden(false);
        }
        if (signerEdit->text().length() == 0) {
            moduleInfo->itemAt(24)->widget()->setHidden(true);
            moduleInfo->itemAt(25)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(24)->widget()->setHidden(false);
            moduleInfo->itemAt(25)->widget()->setHidden(false);
        }
        if (sig_keyEdit->text().length() == 0) {
            moduleInfo->itemAt(26)->widget()->setHidden(true);
            moduleInfo->itemAt(27)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(26)->widget()->setHidden(false);
            moduleInfo->itemAt(27)->widget()->setHidden(false);
        }
        if (sig_hashalgoEdit->text().length() == 0) {
            moduleInfo->itemAt(28)->widget()->setHidden(true);
            moduleInfo->itemAt(29)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(28)->widget()->setHidden(false);
            moduleInfo->itemAt(29)->widget()->setHidden(false);
        }
        if (signatureEdit->text().length() == 0) {
            moduleInfo->itemAt(30)->widget()->setHidden(true);
            moduleInfo->itemAt(31)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(30)->widget()->setHidden(false);
            moduleInfo->itemAt(31)->widget()->setHidden(false);
        }
        if (aliasEdit->text().length() == 0) {
            moduleInfo->itemAt(32)->widget()->setHidden(true);
            moduleInfo->itemAt(33)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(32)->widget()->setHidden(false);
            moduleInfo->itemAt(33)->widget()->setHidden(false);
        }
        if (parmEdit->text().length() == 0) {
            moduleInfo->itemAt(34)->widget()->setHidden(true);
            moduleInfo->itemAt(35)->widget()->setHidden(true);
        } else {
            moduleInfo->itemAt(34)->widget()->setHidden(false);
            moduleInfo->itemAt(35)->widget()->setHidden(false);
        }
    });
}
